package cn.tedu.test.rpc.demo02;


import cn.tedu.test.rpc.common.IUserService;
import cn.tedu.test.rpc.common.User;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Demo02Server {
    private static boolean isRunning=true;
    public static void main(String[] args) throws Exception {
        ServerSocket serverSocket=new ServerSocket(8000);
        while(isRunning){
            Socket socket=serverSocket.accept();
            process(socket);
            socket.close();
        }
        serverSocket.close();
    }
    private static void process(Socket socket) throws Exception{
        InputStream inputStream=socket.getInputStream();
        OutputStream outputStream = socket.getOutputStream();
        DataInputStream dataInputStream=new DataInputStream(inputStream);
        DataOutputStream dataOutputStream=new DataOutputStream(outputStream);
        long id=dataInputStream.readLong();
        IUserService userService=new UserServiceImpl();
        User user= userService.findUserById(id);
        dataOutputStream.writeLong(user.getId());
        dataOutputStream.writeUTF(user.getUsername());
        dataOutputStream.flush();
    }
}
